import 'package:flutter/material.dart';
import 'package:wallpaper/components/my_tab_scroll_header.dart';
import 'package:wallpaper/pages/other/suyan/random_video_list.dart';

class RandomVideo extends StatefulWidget {
  const RandomVideo({super.key});

  @override
  State<RandomVideo> createState() => _RandomVideoState();
}

class _RandomVideoState extends State<RandomVideo>
    with TickerProviderStateMixin {
  List childTypes = [];
  late TabController tabController;
  int activeIndex = 0;
  PageController pageController = PageController(initialPage: 0);
  List types = [
    [1, '动漫'],
    [2, '网红'],
    [3, '游戏'],
    [4, '热门'],
    [5, '风景'],
    [6, '其他'],
    [7, '热舞'],
    [8, '娱乐'],
    [9, '影视'],
    [10, '动物'],
  ];
  List<String> subList = [];

  @override
  void initState() {
    super.initState();
    subList = types.map((e) => e[1].toString()).toList();
    tabController = TabController(length: subList.length, vsync: this);
    tabController.addListener(() {
      setState(() {
        activeIndex = tabController.index;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        backgroundColor: theme.primaryContainer,
        surfaceTintColor: theme.primaryContainer,
        elevation: 0,
        title: MyTabScrollHeader(
            tabController: tabController,
            tabs: subList,
            onTap: (index) {
              pageController.jumpToPage(index);
            }),
      ),
      body: Column(
        children: [
          Expanded(
              child: PageView(
            controller: pageController,
            children: [
              for (var i = 0; i < subList.length; i++)
                RandomVideoList(
                  format: types[i][0],
                ),
            ],
            onPageChanged: (index) {
              setState(() {
                activeIndex = index;
              });
              tabController.animateTo(index);
            },
          ))
        ],
      ),
    );
  }
}
